﻿using DevComponents.DotNetBar.SuperGrid;
using SDATA;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SABI
{
    public partial class frm0040 : Form
    {
        public frm0040()
        {
            InitializeComponent();
        }

        string sql = "";
        string _BankAccountID = "";
        DataTable dt_Grid1 = new DataTable();

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Form frm = new frm0050();
            frm.ShowDialog();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHome"] as frmHOME).CloseTab(this);
        }

        private void frm0040_Shown(object sender, EventArgs e)
        {
            loadDATA();
        }
        
        public void loadDATA()
        {
            sql = "SELECT * FROM tb0034 ORDER BY BankAccountID";
            dt_Grid1 = FRM.GetDataTable(sql);
            GridDB1.PrimaryGrid.DataSource = dt_Grid1;
            foreach (GridColumn gc in GridDB1.PrimaryGrid.Columns)
                gc.FilterMatchType = FilterMatchType.Wildcards;
        }        

        private void frm0040_Load(object sender, EventArgs e)
        {

        }

        void nAdd()
        {
            Form frm = new frm0050();
            frm.ShowDialog();
        }
        void nEdit()
        {
            Form frm = new frm0050(false, false, _BankAccountID);
            frm.ShowDialog();
        }
        void nDelete()
        {
            if (GridDB1.PrimaryGrid.GetSelectedRows().Count < 1)
            {
                SDLL.DLL02.Alert("Bạn phải chọn dòng cần xóa!");
                return;
            }
            if (!SDLL.DLL02.Confirm("Bạn có chắc muốn xóa?", "Xác nhận")) { return; }

            string sql = "DELETE tb0034 WHERE BankAccountID='{0}'";
            sql = string.Format(sql, _BankAccountID);
            if (DATA.ExecuteNonQuery(sql, FRM.ConnectString))
            {
                loadDATA();
            }
            else
            {
                SDLL.DLL02.Alert("Xóa dữ liệu không thành công");
            }
        }
        void nView()
        {
            Form frm = new frm0050(false, true, _BankAccountID);
            frm.ShowDialog();
        }
        void nInfo()
        {
            string tn = "tb0034";
            string ws = string.Format("WHERE BankAccountID='{0}'", _BankAccountID);
            Form frm = new frm0067(tn, ws);
            frm.ShowDialog();
        }
        void nRefresh()
        {
            loadDATA();
        }

        private void GridDB1_SelectionChanged(object sender, GridEventArgs e)
        {
            if (GridDB1.PrimaryGrid.GetSelectedRows().Count < 1) { return; }
            GridRow gr = (GridRow)GridDB1.PrimaryGrid.GetSelectedRows()[0];
            DataRowView drv = (DataRowView)gr.DataItem;
            DataTable dt = dt_Grid1.Clone();
            dt.ImportRow(drv.Row);
            _BankAccountID = dt.Rows[0]["BankAccountID"].ToString();
        }

        private void GridDB1_RowDoubleClick(object sender, GridRowDoubleClickEventArgs e)
        {
            if (GridDB1.PrimaryGrid.GetSelectedRows().Count < 1) { return; }
            nEdit();
        }

        private void mAdd_Click(object sender, EventArgs e)
        {
            nAdd();
        }

        private void mEdit_Click(object sender, EventArgs e)
        {
            nEdit();
        }

        private void mDelete_Click(object sender, EventArgs e)
        {
            nDelete();
        }

        private void mView_Click(object sender, EventArgs e)
        {
            nView();
        }

        private void mDuplicate_Click(object sender, EventArgs e)
        {

        }

        private void mRefresh_Click(object sender, EventArgs e)
        {
            nRefresh();
        }

        private void mSystemInfo_Click(object sender, EventArgs e)
        {
            nInfo();
        }
        
    }
}
